package com.siyu.tickets.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.core.web.WebErrors;
import com.siyu.tickets.entity.Element;
import com.siyu.tickets.manager.ElementMng;

/**
 * 基础数据操作
 * 
 * @since 2013-7-11
 * @version 0.1
 * @author cuigx
 */
@Controller
public class ElementAct {

	private static final Logger log = LoggerFactory.getLogger(ElementAct.class);

	@RequestMapping("/element/{type}/list")
	public String list(String name, @PathVariable String type, Boolean enable, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		// 校验类别的有效性
		if (!Element.checkTypeValid(type)) {
			errors.addErrorCode("error.element.invalidType");
		}
		if (!errors.hasErrors()) {
			Pagination pagination = manager.getPage(name, type, enable, SimplePage.cpn(pageNo),
					CookieUtils.getPageSize(request));
			model.addAttribute("name", name);
			model.addAttribute("type", type);
			model.addAttribute("enable", enable);
			model.addAttribute("pagination", pagination);
			model.addAttribute("pageNo", pagination.getPageNo());
			return "element/list";
		}
		errors.toModel(model);
		return null;// TODO
	}

	@RequestMapping("/element/{type}/add")
	public String add(@PathVariable String type, HttpServletRequest request, ModelMap model) {
		model.addAttribute("type", type);
		return "element/add";
	}

	@RequestMapping("/element/{type}/edit")
	public String edit(Integer id, @PathVariable String type, HttpServletRequest request, ModelMap model) {
		Element element = manager.findById(id);
		model.addAttribute("element", element);
		return "element/edit";
	}

	@RequestMapping("/element/{type}/save")
	public String save(Element bean, HttpServletRequest request, ModelMap model) {
		bean.setCreateTime(new Date());
		bean = manager.save(bean);
		return "redirect:list";
	}

	@RequestMapping("/element/{type}/update")
	public String update(Element bean, HttpServletRequest request, ModelMap model) {
		manager.update(bean);
		return "redirect:list";
	}

	@RequestMapping("/element/{type}/delete")
	public String delete(Integer[] ids, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Element[] beans = manager.deleteByIds(ids);
		for (Element bean : beans) {
			log.info("delete Element id={}", bean.getId());
		}
		return "redirect:list";
	}

	@RequestMapping("/element/{type}/order")
	public String priorityUpdate(Integer[] wids, Integer[] orders, HttpServletRequest request, ModelMap model) {
		if (wids == null || wids.length <= 0) {
			return "redirect:list";
		}
		Element t;
		Integer id;
		Integer order;
		for (int i = 0; i < wids.length; i++) {
			id = wids[i];
			order = orders[i];
			if (id != null && order != null) {
				t = manager.findById(id);
				if (t != null) {
					t.setOrder(order);
					manager.update(t);
				}
			}
		}
		return "redirect:list";
	}

	@Autowired
	private ElementMng manager;

}
